var express = require("express");
var router = express.Router();
const productModel = require("../../models/productModel");
const charProductModel = require("../../models/characteristicProductModel");
const categoryModel = require("../../models/categoryModel");

// 根据分类获取农产品数据
router.get("/searchProductByCategoryId", async (req, res) => {
  let { pageNo, pageSize, sort, categoryId } = req.query;
  // 没有分类，获取全部农产品数据
  if (!categoryId) {
    // 按照库存量
    if (sort == 0) {
      productModel
        .find()
        .sort({ quantity: -1 })
        .skip((pageNo - 1) * pageSize)
        .limit(pageSize)
        .then((data) => {
          if (data.length === 0) {
            return res.json({
              code: 200,
              data,
              msg: "数据为空",
            });
          } else {
            return res.json({
              code: 200,
              data,
              msg: "获取数据成功",
              total: data.length,
            });
          }
        })
        .catch((err) => {
          return res.status(500).json({
            code: "1001",
            msg: "数据库读取失败~",
            data: null,
          });
        });
    } else if (sort == 1) {
      // 按照价格
      productModel
        .find()
        .sort({ price: -1 })
        .skip((pageNo - 1) * pageSize)
        .limit(pageSize)
        .then((data) => {
          if (data.length === 0) {
            return res.json({
              code: 200,
              data,
              msg: "数据为空",
            });
          } else {
            return res.json({
              code: 200,
              data,
              msg: "获取数据成功",
              total: data.length,
            });
          }
        })
        .catch((err) => {
          return res.status(500).json({
            code: "1001",
            msg: "数据库读取失败~",
            data: null,
          });
        });
    }
  }
  if (categoryId) {
    let c = await categoryModel.findById(categoryId);
    let category = c.name;
    //   根据库存量排序
    if (sort == 0) {
      productModel
        .find({ category })
        .sort({ quantity: -1 })
        .skip((pageNo - 1) * pageSize)
        .limit(pageSize)
        .then((data) => {
          if (data.length === 0) {
            return res.json({
              code: 200,
              data,
              msg: "数据为空",
              total: data.length,
            });
          }
          return res.json({
            code: 200,
            data,
            msg: "成功获取数据",
            total: data.length,
          });
        })
        .catch((err) => {
          return res.status(500).json({
            code: "1001",
            msg: "数据库读取失败~",
            data: null,
          });
        });
    } else if (sort == 1) {
      // 根据价格排序
      productModel
        .find({ category })
        .sort({ price: -1 })
        .skip((pageNo - 1) * pageSize)
        .limit(pageSize)
        .then((data) => {
          if (data.length === 0) {
            return res.json({
              code: 200,
              data,
              msg: "数据为空",
              total: data.length,
            });
          }
          return res.json({
            code: 200,
            data,
            msg: "成功获取数据",
            total: data.length,
          });
        })
        .catch((err) => {
          return res.status(500).json({
            code: "1001",
            msg: "数据库读取失败~",
            data: null,
          });
        });
    }
  }
});

// 根据id获取
router.get("/:_id", async (req, res) => {
  try {
    let { _id } = req.params;
    let charProduct = await charProductModel.findById(_id);
    let product = await productModel.findById(_id);
    if (!charProduct && !product) {
      return res.json({
        code: -1,
        data: null,
        msg: "没有该农产品",
      });
    } else if (!product) {
      return res.json({
        code: 200,
        data: charProduct,
        msg: "成功获取产品信息",
      });
    } else {
      return res.json({
        code: 200,
        data: product,
        msg: "成功获取产品信息",
      });
    }
  } catch (err) {
    res.status(500).json({ message: err.message });
  }
});

module.exports = router;
